<template>
  <BasicTable @register="registerTable" @selection-change="selectionChange">
    <template #toolbar>
      <div class="button-box">
        <RejectProcess
          :taskId="taskId"
          :processId="processId"
          @close="reload"
          @restart="restartProcess"
          class="mr-2"
          ><a-button v-auth="'processtasks:withdraw'">{{ t('撤回') }}</a-button></RejectProcess
        >
        <LookProcess :taskId="taskId" :processId="processId" @close="reload"
          ><a-button v-auth="'processtasks:view'">{{ t('查看') }}</a-button></LookProcess
        >
      </div>
    </template>

    <template #currentProgress="{ record }">
      <a-progress v-if="record.currentProgress" :percent="record.currentProgress" size="small" />
    </template>
  </BasicTable>
  <LaunchProcess
    v-if="restartProcessVisible"
    :schemaId="schemaId"
    :taskId="taskId"
    @close="restartProcessClose"
  />
</template>

<script setup lang="ts">
  import userTaskTable from './../../hooks/userTaskTable';

  import { ref } from 'vue';

  import LookProcess from './../LookProcess.vue';
  import LaunchProcess from './../LaunchProcess.vue';
  import RejectProcess from './../RejectProcess.vue';

  import { BasicTable, useTable, BasicColumn } from '/@/components/Table';
  import { getSchemaTask } from '/@/api/workflow/process';
  import { TaskTypeUrl } from '/@/enums/workflowEnum';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const restartProcessVisible = ref(false);
  const configColumns: BasicColumn[] = [
    {
      title: t('流水号'),
      dataIndex: 'serialNumber',
      width: 80,
    },
    {
      title: t('流程名称'),
      dataIndex: 'processName',
      width: '32%',
      align: 'left',
    },
    {
      title: t('任务名称'),
      dataIndex: 'currentTaskName',
      width: '17%',
      align: 'left',
    },
    {
      title: t('当前进度'),
      dataIndex: 'currentProgress',
      width: '17%',
      slots: { customRender: 'currentProgress' },
    },
    {
      title: t('发起人'),
      dataIndex: 'originator',
      align: 'left',
      width: 80,
    },
    {
      title: t('发起时间'),
      dataIndex: 'createTime',
      align: 'left',
      width: 120,
    },
  ];

  const { formConfig, processId, taskId, schemaId, selectionChange } = userTaskTable();
  const [registerTable, { reload }] = useTable({
    title: t('已办任务列表'),
    api: getSchemaTask,
    rowKey: 'id',
    columns: configColumns,
    formConfig: formConfig(),
    beforeFetch: (params) => {
      return { data: params, taskUrl: TaskTypeUrl.FINISHED_TASKS };
    },
    rowSelection: {
      type: 'radio',
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    pagination: {
      pageSize: 18,
    },
    indexColumnProps: {
      width: 50,
    },
  });

  function restartProcess() {
    restartProcessVisible.value = true;
  }
  function restartProcessClose() {
    restartProcessVisible.value = false;
    reload();
  }
</script>

<style scoped></style>
